<?php

$container_bl=join('&nbsp;', $form->_button_container['BL']);
$container_br=join('&nbsp;', $form->_button_container['BR']);
$container_tr=join('&nbsp;', $form->_button_container['TR']);

if ($form->_status=='delete' || $form->_action=='delete' || $form->_status=='unknow_record'):
	echo $form->output;
else:

$campos=$form->template_details('itconv');
$scampos  ='<tr id="tr_itconv_<#i#>">';
$scampos .='<td class="littletablerow" align="left" >'.$campos['codigo']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['descrip']['field'].'</td>';
$scampos .='<td class="littletablerow" align="right">'.$campos['entrada']['field'].  '</td>';
$scampos .='<td class="littletablerow" align="right">'.$campos['salida']['field'];
$scampos .= $campos['costo']['field'].'</td>';
$scampos .= '<td class="littletablerow"><a href=# onclick="del_itconv(<#i#>);return false;">'.img("images/delete.jpg").'</a></td></tr>';
$campos=$form->js_escape($scampos);

if(isset($form->error_string)) echo '<div class="alert">'.$form->error_string.'</div>';

//echo $form_scripts;
echo $form_begin;
if($form->_status!='show'){ ?>

<script language="javascript" type="text/javascript">
var itconv_cont=<?php echo $form->max_rel_count['itconv']; ?>;

$(function(){
	$(".inputnum").numeric(".");
	for(var i=0;i < <?php echo $form->max_rel_count['itconv']; ?>;i++){
		autocod(i.toString());
	};
});

//Agrega el autocomplete
function autocod(id){
	$('#codigo_'+id).autocomplete({
		source: function( req, add){
			$.ajax({
				url:  "<?php echo site_url('ajax/buscasinv'); ?>",
				type: "POST",
				dataType: "json",
				data: "q="+req.term,
				success:
					function(data){
						var sugiere = [];
						$.each(data,
							function(i, val){
								sugiere.push( val );
							}
						);
						add(sugiere);
					},
			})
		},
		minLength: 2,
		select: function( event, ui ) {
			$('#codigo_'+id).val(ui.item.codigo);
			$('#descrip_'+id).val(ui.item.descrip);
			$('#descrip_'+id+'_val').text(ui.item.descrip);
			$("#costo_"+id).val(ui.item.ultimo);
			$('#entrada_'+id).focus();
		}
	});
}

function validaEnt(i){
	var entrada = Number($("#entrada_"+i).val());
	if(entrada>0)
		$("#salida_"+i).val('0');
	$("#entrada_"+i).val(entrada);
}

function validaSalida(i){
	var salida =Number($("#salida_"+i).val());
	if(salida>0)
		$("#entrada_"+i).val('0');
	$("#salida_"+i).val(salida)
}

function add_itconv(){
	var htm = <?php echo $campos; ?>;
	can = itconv_cont.toString();
	con = (itconv_cont+1).toString();
	htm = htm.replace(/<#i#>/g,can);
	htm = htm.replace(/<#o#>/g,con);
	$("#__UTPL__").before(htm);
	$("#entrada_"+can).numeric(".");
	$("#salida_"+can).numeric(".");
	//$("#costo_"+can).numeric(".");
	autocod(can);
	itconv_cont=itconv_cont+1;
}

function post_modbus_sinv(nind){
	id=nind.toString();
	descrip=$('#descrip_'+id).val();
	$('#descrip_'+id+'_val').text(descrip);
	$('#entrada_'+id).focus();
}

function del_itconv(id){
	id = id.toString();
	$('#tr_itconv_'+id).remove();
}
</script>
<?php } ?>
<table align='center' width="85%" style='background: #EEEEEE' >
	<tr>
<?php if ($form->_status=='show') { ?>
		<td>
			<a href="#" onclick="window.open('<?php echo base_url() ?>formatos/verhtml/CONV/<?php echo $form->numero->value ?>', '_blank', 'width=800, height=600, scrollbars=Yes, status=Yes, resizable=Yes, screenx='+((screen.availWidth/2)-400)+',screeny='+((screen.availHeight/2)-300)+'');" heigth="600" ><img src='<?php echo base_url() ?>images/html_icon.gif'></a>
			<a href="#" onclick="window.open('<?php echo base_url() ?>formatos/ver/CONV/<?php echo $form->numero->value ?>', '_blank', 'width=800, height=600, scrollbars=Yes, status=Yes, resizable=Yes, screenx='+((screen.availWidth/2)-400)+',screeny='+((screen.availHeight/2)-300)+'');" heigth="600" ><img src='<?php echo base_url() ?>images/pdf_logo.gif'></a> Comprobante  
		<td>
		 <?php } ?>
        <td width="45%" align="right"><?php echo $container_tr?></td>
	</tr>
</table>
<table align='center' width="85%">
	<tr>
		<td>
		<fieldset style='border: 2px outset #9AC8DA;background: #FFFFFF;'>
		<legend class="titulofieldset" style='color: #2E4B77;'>N&uacute;mero de Conversi&oacute;n <b><?php if($form->_status=='show' or $form->_status=='modify' ) echo $form->numero->output; ?></b></th></legend>
          <table width="100%"  style="margin:0;width:100%;">
            <tr>
              <td width="10%" class="littletableheader"><?=$form->fecha->label ?></td>
              <td width="90%" class="littletablerow"><?=$form->fecha->output ?></td>
            </tr>
            <tr>
              <td class="littletableheader"><?=$form->almacen->label ?></td>
              <td class="littletablerow"><?=$form->almacen->output ?></td>
            </tr>
            <tr>
              <td class="littletableheader"><?=$form->cambio->label ?></td>
              <td class="littletablerow"><?=$form->cambio->output ?></td>
            </tr>
            <tr>
              <td class="littletableheader"><?=$form->observa1->label ?></td>
              <td class="littletablerow"><?=$form->observa1->output ?></td>
            </tr>
        </table>
        </fieldset>
		</td>
	</tr>
	<tr>
		<td>
		<fieldset style='border: 2px outset #9AC8DA;background: #EFEFFF;'>
		<legend class="titulofieldset" style='color: #2E4B77;'>Lista de Art&iacute;culos</legend>
		<table width='100%'>
			<tr class="littletablerow">
				<td bgcolor='#527D9E'>C&oacute;digo</td>
				<td bgcolor='#527D9E'>Descripci&oacute;n</td>
				<td bgcolor='#527D9E'>Entrada</td>
				<td bgcolor='#527D9E'>Salida</td>
				<td bgcolor='#527D9E'>Costo</td>
				<?php if($form->_status!='show') {?>
					<td class="littletableheader">&nbsp;</td>
				<?php } ?>
			</tr>

			<?php for($i=0;$i<$form->max_rel_count['itconv'];$i++) {
				$it_codigo   = "codigo_$i";
				$it_descrip  = "descrip_$i";
				$it_salida   = "salida_$i";
				$it_entrada  = "entrada_$i";
				$it_costo    = "costo_$i";

				$pprecios = $form->$it_costo->output;
			?>

			<tr id='tr_itconv_<?php echo $i; ?>'>
				<td class="littletablerow" align="left" ><?php echo $form->$it_codigo->output; ?></td>
				<td class="littletablerow" align="left" ><?php echo $form->$it_descrip->output;  ?></td>
				<td class="littletablerow" align="right"><?php echo $form->$it_entrada->output;   ?></td>
				<td align="right" class="littletablerow"><?php echo $form->$it_salida->output;  ?></td>
				<td align="right" class="littletablerow"><?php echo $form->$it_costo->output;   ?></td>
				<?php if($form->_status!='show') {?>
				<td class="littletablerow">
					<a href='#' onclick='del_itconv(<?=$i ?>);return false;'><?php echo img("images/delete.jpg"); ?></a>				</td>
				<?php } ?>
			</tr>
			<?php } ?>

			<tr id='__UTPL__'>
				<td id='cueca'></td>
			</tr>
		</table>
		</fieldset>
		<?php echo $container_bl ?>
		<?php echo $container_br ?>
		</td>
	</tr>
	<?php echo $form_end; ?>
</table>
<?php endif; ?>
